#include<bits/stdc++.h>
using namespace std;
int ans,a[200005],cntb,cntc,n,_,dp[2005][2005],f[2005],v[2005][2005];
int calc(int l,int r){
    int res=0;
    for(int i=l+1;i<=r;i++){
        if(a[i]==a[i-1]) res+=a[i];
    }
    return res;
}
void sub1(){
    int res=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            //int val=calc(j,i);
            dp[i][j]=f[j-1]+v[j][i];
            if(i+1<=n&&a[i+1]==a[j-1]) f[i]=max(f[i],dp[i][j]+a[j-1]);
            else f[i]=max(f[i],dp[i][j]);
        }
    }
    for(int i=1;i<=n;i++){
        res=max(res,dp[n][i]);
    }
    cout<<res<<endl;
    return;
    //cout<<1.0*clock()/CLOCKS_PER_SEC<<endl;
}
signed main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>_;
    while(_--){
        cin>>n;
        for(int i=1;i<=n;i++){
            f[i]=0;
            for(int j=1;j<=n;j++){
                dp[i][j]=0;
                v[i][j]=0;
            }
        }
        //memset(f,0,sizeof(f));
        //memset(dp,0,sizeof(dp));
        //memset(v,0,sizeof(v));
        ans=0;cntb=0;cntc=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(a[j]==a[j-1]){
                    v[i][j]=v[i][j-1]+a[j];
                }
                else{
                    v[i][j]=v[i][j-1];
                }
            }
        }
        /*if(n<=15){
            dfs(1);
            cout<<ans<<endl;
            continue;
        }
        else if(n<=100){
            sub1();
        }*/

        sub1();
        
    }
    return 0;
}
/*
3
 3
 1 2 1
 4
 1 2 3 4
 8
 3 5 2 5 1 2 1 4
*/